package com.tiange.controller;

import com.tiange.annotation.MyAnno;
import com.tiange.model.User;
import com.tiange.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/add")
    @ResponseBody
    public Map<String, Object> add() {
        User user = new User();
        user.setName("shiguang");
        user.setAddress("shanghai");
        user.setAge(25);
        userService.insert(user);
        Map<String, Object> req = new HashMap<String, Object>();
        req.put("msg", "success");
        return req;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete() {
        userService.delete(1);

        Map<String, Object> req = new HashMap<String, Object>();
        req.put("msg", "success");
        req.put("body", "");
        return req;
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map<String, Object> update() {
        User user = new User();
        user.setId(1);
        user.setName("qin");
        user.setAddress("guangzhou");
        user.setAge(22);
        userService.update(user);

        Map<String, Object> req = new HashMap<String, Object>();
        req.put("msg", "success");
        req.put("body", "");
        return req;
    }

    @RequestMapping("/getUser")
    @ResponseBody
    @MyAnno
    public Map<String, Object> getUser() {
        User user = userService.getUser(1);
        System.out.println(user);
        Map<String, Object> req = new HashMap<String, Object>();
        req.put("msg", "success");
        req.put("body", user);
        return req;
    }

    @RequestMapping("/listUser")
    @ResponseBody
    public Map<String, Object> listUser() {
        List<User> list = userService.listUser();
        System.out.println(list);
        Map<String, Object> req = new HashMap<String, Object>();
        req.put("msg", "success");
        req.put("body", list);
        return req;
    }
}
